<!--  -->
<template>
  <div class="house">
    房源管理
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="销售楼盘" name="first">
        <div class="search">
          <!-- <el-select v-model="value" placeholder="请选择省份">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-select v-model="value1" placeholder="请选择城市">
            <el-option
              v-for="item in options1"
              :key="item.value1"
              :label="item.label1"
              :value="item.value1"
            >
            </el-option>
          </el-select>
          <el-select v-model="value2" placeholder="请选择县区">
            <el-option
              v-for="item in options2"
              :key="item.value2"
              :label="item.label2"
              :value="item.value2"
            >
            </el-option>
          </el-select> -->
          <!-- 三级联动 -->
          <v-distpicker @selected="onSelected"></v-distpicker>
          <van-search v-model="search" @input="onSearch" placeholder="关键字" />
          <el-button type="primary" @click="handleSearch()" class="search-btn"
            >搜索</el-button
          >
        </div>
        <el-button type="primary" @click="handleAdd()" class="add-btn">
          新增
        </el-button>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序号" width="180" prop="id" />
          <el-table-column label="楼盘名称" width="180" prop="name" />
          <el-table-column label="所属区域" width="180" prop="address" />
          <el-table-column label="价格（元/m²）" width="180" prop="num" />
          <el-table-column label="更新时间" width="220" prop="date" />
          <el-table-column label="排序" width="180" prop="sort" />
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.flag"
                active-color="#36c6d3"
                active-value="true"
                inactive-value="false"
                @change="active_text($event, scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleDelete(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- <a-table :columns="columns" :data-source="tableData">
          <span slot="type">
            <a-switch default-checked @change="onChange"
          /></span>
          <span slot="action" slot-scope="record">
            <p @click="edit(record)">编辑</p>
            <p @click="del(record.id)">删除</p>
          </span>
        </a-table> -->
      </el-tab-pane>
      <el-tab-pane label="房屋租凭" name="second">
        房屋租凭
      </el-tab-pane>
    </el-tabs>
    <!-- 遮罩 -->
    <el-dialog title="" :visible.sync="dialog" width="30%">
      <el-form v-model="obj">
        <el-form-item label="楼盘名称" :label-width="formLabelWidth">
          <el-input v-model="obj.name" placeholder="楼盘名称"></el-input>
        </el-form-item>
        <el-form-item label="所属区域" :label-width="formLabelWidth">
          <el-input v-model="obj.address" placeholder="所属区域"></el-input>
        </el-form-item>
        <el-form-item label="价格（元/m²）" :label-width="formLabelWidth">
          <el-input v-model="obj.num" placeholder="价格"></el-input>
        </el-form-item>
        <el-form-item label="更新时间" :label-width="formLabelWidth">
          <el-input v-model="obj.date" placeholder="价格"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="obj.sort" placeholder="价格"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="false">取 消</el-button>
        <el-button type="primary" @click="handleSure()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// const columns = [
//   {
//     title: "序号",
//     dataIndex: "id",
//     key: "id",
//   },
//   {
//     title: "楼盘名称",
//     dataIndex: "name",
//     key: "name",
//   },
//   {
//     title: "所属区域",
//     dataIndex: "address",
//     key: "address",
//   },
//   {
//     title: "价格",
//     dataIndex: "price",
//     key: "price",
//   },
//   {
//     title: "排序",
//     dataIndex: "sort",
//     key: "sort",
//   },
//   {
//     title: "type",
//     key: "type",
//     scopedSlots: { customRender: "type" },
//   },
//   {
//     title: "Action",
//     key: "action",
//     scopedSlots: { customRender: "action" },
//   },
// ];
import {
  getHousingList,
  delHousingList,
  editHousingList,
  addHousingList,
  cele,
} from "@/api";
import VDistpicker from "v-distpicker";
import debounce from "../../../utils/debounce";
export default {
  components: {
    VDistpicker,
  },
  // 组件状态值
  data() {
    return {
      // columns,
      //遮罩开关
      dialog: false,
      flag: true,
      formLabelWidth: "120px",
      //遮罩数据
      obj: {
        name: "",
        address: "",
        num: "",
        date: "",
        sort: "",
      },
      //状态开关
      value: true,
      //分页数据
      page: {
        size: 10,
        current: 1,
        total: 0,
      },
      //表格数据
      tableData: [],
      //搜索框的值
      search: "",
      //tab切换
      activeName: "first",
    };
  },
  // 计算属性
  computed: {},
  // 组件方法
  methods: {
    //获取房源数据
    async getList() {
      const res = await getHousingList({ ...this.page, value: this.search });
      console.log(res);
      this.tableData = res.data.data;
    },
    //tab切换
    handleClick(tab, event) {
      console.log(tab, event);
    },
    onSearch() {
      debounce(() => {
        this.getList();
      }, 500);
    },
    //搜索
    handleSearch(search) {
      this.getList(search);
    },
    //添加
    handleAdd() {
      this.dialog = true;
      this.flag = false;
    },
    //编辑回显
    handleEdit(row) {
      this.dialog = true;
      this.flag = true;
      this.obj.name = row.name;
      this.obj.address = row.address;
      this.obj.num = row.num;
      this.obj.date = row.date;
      this.obj.sort = row.sort;
      this.obj.id = row.id;
    },
    //遮罩确定
    handleSure() {
      if (this.flag) {
        editHousingList(this.obj).then((res) => {
          console.log(res);
          this.getList();
        });
      } else {
        addHousingList(this.obj).then((res) => {
          console.log(res);
          this.getList();
        });
      }
      this.clear();
    },
    //清除遮罩
    clear() {
      this.dialog = false;
      this.obj.name = "";
      this.obj.address = "";
      this.obj.num = "";
      this.obj.date = "";
      this.obj.sort = "";
    },
    //删除
    handleDelete(id) {
      delHousingList({ id }).then((res) => {
        console.log(res);
        this.getList();
      });
    },
    //开关
    active_text(value, row) {
      console.log(value, row);
    },
    //三级联动
    onSelected(data) {
      console.log(data);
    },
  },
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    this.getList();
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>
.house {
  width: 100%;
  height: 100%;
  background: rgb(240, 240, 240);
  padding: 5px 10px;
}
.el-tabs {
  background: #fff;
  #pane-first {
    padding: 0 10px;
  }
  #pane-second {
    padding: 0 10px;
  }
}
.search {
  width: 100%;
  display: flex;
  align-items: center;
}
.el-select {
  margin-right: 10px;
}
.add-btn {
  width: 80px;
  margin: 4px 0;
}
</style>
